/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.entity.Product;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Nam
 */
public class LazyProductDataModel extends LazyDataModel<Product> {

    private IProductManager productManager;

    public LazyProductDataModel() {
    }

    @Override
    public Product getRowData(String rowKey) {
        long id = Long.parseLong(rowKey);
        return productManager.findProductById(id);
    }

    @Override
    public Object getRowKey(Product object) {
        return object.getId();
    }

    public LazyProductDataModel(IProductManager productManager) {
        this.productManager = productManager;
    }

    public void setProductManager(IProductManager productManager) {
        this.productManager = productManager;
    }

    @Override
    public List<Product> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filter) {
        Map<String, Object> myFilter = new HashMap<String, Object>(filter);
        setRowCount((int) productManager.countProduct(myFilter));
        if (sortOrder == SortOrder.DESCENDING) {
            return productManager.findProductPage(first, pageSize, sortField, true, myFilter);
        } else {
            return productManager.findProductPage(first, pageSize, sortField, false, myFilter);
        }
    }
}
